package com.uma.hadoop.practice;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


import java.io.InputStream;
import java.net.URI;

/**
 * @ClassName FileSystemCat
 * @Description 直接使用FileSystm以标准格式显示Hadoop文件系统中的文件
 * @Author zhaolu
 * @Date 2021/2/13 7:04 PM
 * @Version 1.0
 **/
public class FileSystemCat {


    private static final String HDFSURI = "hdfs://iZ8vbcrhpgexjjl584b3zoZ:9000";

    public static void main(String[] args) throws Exception
    {
        String fileName = "/user/hdfs/input/core-site.xml";
        StringBuilder sb = new StringBuilder();
        sb.append(HDFSURI);
        sb.append(fileName);

        System.setProperty("HADOOP_USER_NAME", "hdfs");

        Configuration conf  = new Configuration();
        FileSystem fs  = FileSystem.get(URI.create(sb.toString()),conf);

        InputStream in = null;
        try{
            in = fs.open(new Path(sb.toString()));

            IOUtils.copyBytes(in,System.out,4096,false);
        }finally {
            IOUtils.closeStreams(in);
        }
    }
}
